////////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2008 Chris Colinsky
// All Rights Reserved
//
// version	0.4
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

import sugarcookie.logger.Logger;

class sugarcookie.tracking.ApacheTracker {
	private static var _instance:ApacheTracker = null;
	private var _trackingImgURL:String;
	private var _log:Logger;
	private var _trackingContainer:MovieClip;
	private var _file:MovieClipLoader;
	
	public static function getInst(tImgURL:String):ApacheTracker {
		return ApacheTracker(_instance?_instance:_instance = new ApacheTracker(tImgURL));
	}
	
	private function ApacheTracker(tImgURL:String) {
		_log = Logger.getLog();
		_trackingImgURL = tImgURL!=undefined?tImgURL:"img/tracking.png";
		
		_trackingContainer = _level0.createEmptyMovieClip("tracking",10000);
		_trackingContainer._x -= 5000;
		_trackingContainer._y -= 5000;
		_trackingContainer._visible = false;
		
		_file = new MovieClipLoader();
		_file.addListener(this);
	}
	
	public function track(id:String):Void {
		if (id==undefined) {
			_log.error("ApacheTracker - track: no page id was passed.");
			return;
		}
		var track_str:String = _trackingImgURL+"?pageID="+id+"&cb="+String(Math.random());
		_log.debug(track_str);
		_file.loadClip(track_str,_trackingContainer);
	}
	
	private function onLoadError(target_mc:MovieClip, errorCode:String, httpStatus:Number):Void {
		_log.error("ApacheTracker - unable to load tracking image.  errorCode="+errorCode+", httpStatus="+httpStatus);
	}
}